<?php
require_once('global.php');

//权限检查
userission();

// 检查站点名字是否符合逻辑
function checksitename($sitename) {
	if(!$sitename || strlen($sitename) > 30) {
		$result = '站点名不能空并不能大于30个字符<br />';
		return $result;
	}
	elseif(eregi("[<>{}(),%#|^&!`$]",$sitename)) {
		$result = '站点名中不能含有特殊字符<br />';
		return $result;
	}
}

// 检查站点描述是否符合逻辑
function checknote($note = '') {
	if($note && strlen($note) > 200) {
		$result = '站点描述不能大于200个字符<br />';
		return $result;
	}
}

//添加站点

if($_POST['action'] == 'doaddsite') {
  	$site_title = addcslashes(trim($_POST["title"]));
  	$site_site_url = trim($_POST["url"]);
  	$site_rss_url = trim($_POST["rss_url"]);
  	$site_icon = trim($_POST["icon"]);
  	$rss_cate = trim($_POST["rss_cate"]);
 	$site_count = trim($_POST["count"]);
  	$site_content = addcslashes(trim($_POST["content"]));
	$state = $options[usercheck];
	$keywords    = str_replace(',,', ',', $keywords);	
	if (substr($keywords, -1) == ',') {
		$keywords = substr($keywords, 0, strlen($keywords)-1);
	}
	$result  = checksitename($site_title);

	//$result .= checkurl($site_site_url,0);
	$result .= checkurl($site_rss_url,0);
	//$result .= checkurl($site_icon,0);

	$result .= checknote($content);
	//$result .= checkkeywords($keywords);

	if($result) {
		redirect($result);
	}
	$site_title    = char_cv($site_title);
	$site_site_url     = char_cv($site_site_url);
	$site_rss_url     = char_cv($site_rss_url);
	$site_icon     = char_cv($site_icon);
	$site_content    = char_cv($site_content);

	$rs = $db->fetch_one_array("SELECT count(*) AS sites FROM {$tablepre}xna_site WHERE site_title='$site_title' AND site_url='$site_url'");
    if($rs['sites'])	{
        redirect('该站点在数据库中已存在', 'index.php?job=site');

    }
  $query = $db->query("INSERT INTO {$tablepre}xna_site
   		(uid,site_title,site_url,rss_url,site_icon,rss_cate,site_email,site_count,site_content,keywords,addtime)
	VALUES
		 ('$ixnauid','$site_title','$site_site_url','$site_rss_url','$site_icon','$rss_cate','$site_email','$site_count','$site_content','$keywords','$timestamp')");
	$siteid = $db->insert_id();
	// 插入/更新Tags
	if ($keywords) {
		$tagdb = explode(',', $keywords);
		$tagnum = count($tagdb);
		for($i=0; $i<$tagnum; $i++) {
			$tagdb[$i] = trim($tagdb[$i]);
			if ($tagdb[$i]) {
				$tag  = $db->fetch_one_array("SELECT tagid,aids FROM {$tablepre}xna_tags WHERE tag='$tagdb[$i]'");
				if(!$tag) {
					$db->query("INSERT INTO {$tablepre}xna_tags (tag,uid,usenum,aids) VALUES ('$tagdb[$i]','$ixnauid', '1', '$siteid')");

					$db->query("UPDATE {$tablepre}xna_statistics SET tag_count=tag_count+1");

				} else {						
					$aids = $tag['aids'].','.$articleid;
					$db->query("UPDATE {$tablepre}xna_tags SET usenum=usenum+1, aids='$aids' WHERE tag='$tagdb[$i]'");
				}
			}
			unset($aids);
		}
	}
    redirect('添加站点成功', 'myfeeds.php');

}
//修改站点

if($action == 'domodsite') {
	$site_title = addcslashes(trim($_POST["title"]));
  	$site_url = trim($_POST["url"]);
  	$site_rss_url = trim($_POST["rss_url"]);
  	$site_icon = trim($_POST["icon"]);
  	$rss_cate = trim($_POST["rss_cate"]);
  	$site_count = trim($_POST["count"]);
  	$site_content = addcslashes(trim($_POST["content"]));
	$cid    = intval($_POST['cid']);
	$keywords    = str_replace(',,', ',', $keywords);	
	if (substr($keywords, -1) == ',') {
		$keywords = substr($keywords, 0, strlen($keywords)-1);
	}
	$result = checksitename($site_title);
	//$result .= checkkeywords($keywords);
	if($result)	{redirect($result);}

	$site_title = char_cv($site_title);
	$rs = $db->fetch_one_array("SELECT count(*) AS categories FROM {$tablepre}xna_category WHERE cid!='$cid' AND cate_title='$site_title'");
    if($rs['categories']) {
		redirect('已经有其他站点使用'.$site_title.'这个名称');
    }

	// 更新站点
   $db->query("UPDATE {$tablepre}xna_site SET
		    site_title='$site_title' ,
		    site_url='$site_url' ,
		    rss_url='$site_rss_url' ,
		    site_icon='$site_icon' ,
		    rss_cate='$rss_cate',
		    site_count='$site_count',
		     keywords='$keywords', 
		    site_content='$site_content'
		WHERE siteid='$sid'");
	//categories_recache();

    redirect('修改站点成功', 'myfeeds.php');
}

//批量处理

if($_POST['action'] == 'domoresite') {
	if($ids = implode_ids($_POST['delete'])) {
		$db->query("DELETE FROM	{$tablepre}xna_site WHERE siteid IN ($ids)");
	}
	if(is_array($_POST['displayorder'])) {
		foreach($_POST['displayorder'] as $siteid => $value) {
			$db->query("UPDATE {$tablepre}xna_site SET displayorder='".intval($_POST['displayorder'][$siteid])."', state='".intval($_POST['state'][$siteid])."' WHERE siteid='".intval($siteid)."'");
			$db->query("UPDATE {$tablepre}xna_feed SET state='".intval($_POST['state'][$siteid])."' WHERE siteid='".intval($siteid)."'");
		}
	}
	//categories_recache();

    redirect('站点已成功更新', 'myfeeds.php');

}


if(!$action) {
	$action = 'list';
}
//站点操作
if (in_array($action, array('addsite', 'modsite', 'delcate','dofetch'))) {
	if ($action == 'addsite') {
		$subnav = '添加站点';
        $site["cid"]=intval($_GET['cid']);
	} elseif($action == 'dofetch'){
		$subnav = '聚合站点';
		$sid=intval($_GET['sid']);
		ob_start();
		doFetch($sid);
		}else {
		$site = $db->fetch_one_array("SELECT * FROM {$tablepre}xna_site WHERE siteid='".intval($_GET['sid'])."'");
		if($action == 'modsite') {
			$subnav = '编辑站点';
		} else {
			$subnav = '删除分类';
		}
	}
}

if ($action == 'list') {
	if($page) {
		$start_limit = ($page - 1) * 30;
	} else {
		$start_limit = 0;
		$page = 1;
	}
	$tatol = $stats['site_count'];	
	$query = $db->query("SELECT * FROM {$tablepre}xna_site WHERE uid='$ixnauid' ORDER BY siteid DESC LIMIT $start_limit, 30");
	$multipage = multi($tatol, 30, $page, 'index.php?job=site&action=list'.$pagelink);
	$sitedb = array();
	while ($site = $db->row($query)) {
		if ($site['state'] == '1') {
			$site['state'] = '<option value="1" selected>显示</option><option value="0">隐藏</option>';
		} else {
			$site['state'] = '<option value="1">显示</option><option value="0" selected>隐藏</option>';
		}
		$site[addtime] = date("Y-m-d ",$site["addtime"]);
		$site[uptime] = date("Y-m-d h:m:s",$site["uptime"]);
		$sitedb[] = $site;
	}
	unset($site);
	$db->free($query);
	$subnav = '编辑站点';
}

$navlink_L = ' &raquo; <a href="myfeeds.php">我的Feeds</a>'.($subnav ? ' &raquo; '.$subnav : '');
include template("myfeeds", _ROOT . "/template/digg/");

?>